package controller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Enterprise;
import model.UserAccount;
import util.Log;
import validator.EnterpriseValidator;
import web.JSONMessage;
import db.ImbiConnection;

public class EnterpriseController extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		if(request.getSession().getAttribute("user") != null){
			int edit;
			try{
				edit = Integer.parseInt(request.getParameter("edit"));
				if(edit <= 0) throw new NumberFormatException();
				handleEdit(request, response, edit);
			} catch(NumberFormatException | NullPointerException e){
				handleViewAll(request, response);
			}						
		} else {
			request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
		}				
	}

	private void handleViewAll(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("enterprise", ImbiConnection.getEnterpriseDAO().findAll());
		request.getRequestDispatcher("/WEB-INF/enterprise.jsp").forward(request, response);
	}

	private void handleEdit(HttpServletRequest request,
			HttpServletResponse response, int edit) throws ServletException,
			IOException {
		Enterprise e = ImbiConnection.getEnterpriseDAO().find(edit);			
		if(e != null){
			request.setAttribute("enterprise", e);					
			request.setAttribute("persons", ImbiConnection.getPersonDAO().findAll());
			request.setAttribute("relationTypes", ImbiConnection.getEntPerRelationTypeDAO().findAll());
			request.getRequestDispatcher("/WEB-INF/enterpriseedit.jsp").forward(request, response);
		} else {
			request.getRequestDispatcher("/WEB-INF/error.jsp").forward(request, response);
		}
	}	
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {		
		UserAccount u = (UserAccount)request.getSession().getAttribute("user");
		if(u != null){			
			EnterpriseValidator validator = new EnterpriseValidator(
					request.getParameter("id"),
					request.getParameter("name"),
					request.getParameter("fullname")
			);
			if(validator.isValid()){
				Enterprise e = ImbiConnection.getEnterpriseDAO().find(validator.getId());
				if(e != null){
					e.setUpdated(new Date());
					e.setUpdatedBy(u.getEmployee().getId());
					e.setName(validator.getName());
					e.setFullName(validator.getFullname());
					ImbiConnection.getEnterpriseDAO().update(e);
					Log.write(u.getUsername()+" updated enterprise "+e.getId());
					response.getWriter().write(JSONMessage.message("Update Successful!"));
				}				
			} 
			else response.getWriter().write(JSONMessage.errorMessage(validator.getErrorMessage()));
		}
	}
}